<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        $layout = $this->_helper->layout();
        $layout->setLayout('layout');
    }

    public function indexAction()
    {
        
    }

    /**
     * TODO: corrixir problema coas vistas: index, indexsearch e search
     * empregar render para search e inxectar no index. Eliminar indexsearch
     */
    public function searchAction()
    {
        $request = $this->getRequest();
        
        $q = $request->getParam('q');
        $this->view->assign('q', $q);
        
        $terms = new Model_Terms();
        $list = $terms->getDefinitions($q, true);
        
        
        if( $request->isXmlHttpRequest() ) {
            // disable layout if it's a Ajax request
            $this->_helper->layout()->disableLayout();
            $this->view->terms = $list;
            
        } else {                        
            $view = new Zend_View();
            $view->setScriptPath(APPLICATION_PATH . '/views/scripts/index');            
            $view->assign('terms', $list);
            $view->assign('q', $q);
            
            $this->view->assign('outputResults', $view->render('search.phtml'));
            
            // render with form
            $this->_helper->viewRenderer->setRender('index');
        }
    }
    
    public function xsearchAction()
    {
        $request = $this->getRequest();
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $q = $request->getParam('q');
        $jsoncallback = $request->getParam('callback', null);
        
        $terms = new Model_Terms();
        $list  = $terms->getDefinitions($q, true);
        
        $json = '{"mensaje": "Sen entradas"}';
        if(count($list)>0) {
            $json = json_encode($list[0]);
        }
        
        header('Content-type: text/javascript');
        
        if(!is_null($jsoncallback)) {
            echo $jsoncallback . '(' . $json . ');';
        } else {
            echo $json;
        }        
    }    

    public function wordAction()
    {
        $request = $this->getRequest();
        
        if( $request->isXmlHttpRequest() ) {
            // disable layout if it's a Ajax request
            $this->_helper->layout()->disableLayout();
        }                
                
        $q = $this->_getParam('q', '');
        $this->view->assign('q', $q);
        
        $terms = new Model_Terms();
        $list = $terms->getDefinitions($q, false);
        if(count($list)>0) {
            $this->view->term = $list[0];
        }       
    }

    public function autocompleteAction()
    {
        // disable layout and view
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $terms = new Model_Terms();
        $data = $terms->terms2json();
        
        header('Content-type: application/json');
        echo $data;
    }

    /**
     *
     * @see http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions
     */
    public function opensearchAction()
    {
        // disable layout and view
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();
        
        $q = $this->_getParam('q', '');
        
        $terms = new Model_Terms();
        $result = $terms->opensearch($q);
        
        header('Content-type: application/x-suggestions+json');
        echo $result;
    }
}











